package ln.fxspringarchetype.dialog;

import javafx.fxml.FXMLLoader;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import org.apache.logging.log4j.util.Strings;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

public abstract class BaseFxDialog<R> extends Dialog<R> {
    /**
     * FXML加载器
     */
    private final FXMLLoader fxmlLoader;

    /**
     * fxml文件路径
     */
    private String fxmlPath;

    /**
     * 窗体图标的路径
     */
    private String windowIconPath;

    public BaseFxDialog() {
        super();
        this.fxmlLoader = createFxmlLoader();
        this.fxmlPath = Strings.EMPTY;
        this.windowIconPath = Strings.EMPTY;
    }

    public FXMLLoader getFxmlLoader() {
        return fxmlLoader;
    }

    public String getFxmlPath() {
        return fxmlPath;
    }

    public void setFxmlPath(String fxmlPath) {
        this.fxmlPath = fxmlPath;
    }

    public String getWindowIconPath() {
        return windowIconPath;
    }

    public void setWindowIconPath(String windowIconPath) {
        this.windowIconPath = windowIconPath;
    }


    /**
     * 创建fxml加载器实例
     *
     * @return 新的fxml实例
     */
    protected FXMLLoader createFxmlLoader() {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setCharset(StandardCharsets.UTF_8);
        return fxmlLoader;
    }

    /**
     * 创建对话框图标
     *
     * @param iconPath 图标路径
     * @return 图标的Image实例
     */
    protected Image createIconImage(String iconPath) {
        return new Image(Objects.requireNonNull(getClass().getResourceAsStream(iconPath)));
    }

    /**
     * 创建默认标签组
     *
     * @return
     */
    protected ButtonType[] createButtonTypes() {
        return new ButtonType[]{
                ButtonType.OK,
                ButtonType.CANCEL
        };
    }

    /**
     * 初始化对话框图标
     */
    protected abstract void initIcon();

    /**
     * 初始化对话框标题
     */
    protected abstract void initTitle();

    /**
     * 初始化对话框按钮
     */
    protected abstract void initButtons();

    /**
     * 加载外部fxml内容
     */
    protected abstract void initContent();

    /**
     * 点击不同按钮返回不同结果
     * Converter返回的结果是通过showAndWait()方法能获取的值
     */
    protected abstract void initResultConverter();
}
